package com.klhappy.houyuan.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.bean.RegisterBean;
import com.klhappy.houyuan.constant.KeyConstant;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.GetPassWordModle;
import com.klhappy.houyuan.utils.GsonRequest;
import com.klhappy.houyuan.view.AbsActivity;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class GetPasswordActivity extends AbsActivity implements CallBackInterModle {

    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.et_id)
    EditText et_id;
    @InjectView(R.id.bt_verification)
    Button bt_verification;


    @InjectView(R.id.et_verification)
    EditText et_verification;
    @InjectView(R.id.et_new_password)
    EditText et_new_password;
    @InjectView(R.id.et_comfirm_password)
    EditText et_comfirm_password;

    @OnClick(R.id.click_ra)
    public void click_ra() {
        finish();
    }
    @OnClick(R.id.bt_verification)
    public void bt_verification() {
        if (count == 60) {
            String userPhone = et_id.getText().toString();
            String telRegex = "[1][0-9]\\d{9}";
            if (userPhone.length() != 11 && !userPhone.matches(telRegex)) {
                Toast.makeText(getApplicationContext(), "请正确输入手机号",
                        Toast.LENGTH_LONG).show();
            } else {
                bt_verification.setClickable(false);
                bt_verification.setBackgroundResource(R.drawable.corner_bg3);
                bt_verification.setTextColor(0xffffffff);
                {
                    refreshReg = false;
                    regist(userPhone);
                }
            }
        }
    }
    @OnClick(R.id.btn_comfirm)
    public void btn_comfirm() {
        String phone = et_id.getText().toString();
        String registCode = et_verification.getText().toString();
        String password = et_new_password.getText().toString();
        String newpass = et_comfirm_password.getText().toString();

        if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(password)
                && !TextUtils.isEmpty(registCode) && !TextUtils.isEmpty(newpass)) {
            if (!password.equals(newpass)) {
                showToast("密码不相符");
            } else {
                showDialog("请稍等...");
                modle.setPassWord(phone, password, registCode);
            }
        } else {
            showToast("请正确输入");
        }
    }

    private int count = 60;
    private Handler handlerBtn = new Handler();
    boolean refreshReg = false;
    private GetPassWordModle modle;
    private String mCookie = null;
    private GsonRequest<RegisterBean> mRequest = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.get_password);
        ButterKnife.inject(this);
        title.setText(R.string.get_password);

        modle = new GetPassWordModle(this);
        modle.setCallBackInterModle(this);
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.NEWPWD_URL)) {
            if (o.getCode() == 200) {
                showToastx(o.getMsg());
                finish();
            } else {
                showToast(o.getMsg());
            }
            closeDialog();
        }
    }

    private void regist(String account) {
        mConfigManage.remove(KeyConstant.cookie);
        HashMap<String, String> map = new HashMap<>();
        map.put("phone", account);
        Response.Listener<RegisterBean> listener = new Response.Listener<RegisterBean>() {
            @Override
            public void onResponse(RegisterBean response) {
                if (response.code == 200){
                    showToastx("验证码发送成功,请稍等...");
                    handler.post(runnable);
                    if (!TextUtils.isEmpty(mCookie)) {
                        mConfigManage.set(KeyConstant.cookie, mCookie);
                    }
                }else {
                    showToastx(response.msg);
                }
            }
        };
        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                showToast("网路错误");
            }
        };
        mRequest = new GsonRequest<RegisterBean>(Request.Method.POST, UrlConstant.PWD_URL, map,
                RegisterBean.class, listener, errorListener) {
            @Override
            protected Response<RegisterBean> parseNetworkResponse(NetworkResponse response) {
                mCookie = getCookie(response);
                Log.e("this", mCookie);
                return super.parseNetworkResponse(response);
            }
        };

        getAppEnv().getVolleyRequest().add(mRequest);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    private Runnable runnable = new Runnable() {

        @Override
        public void run() {
            if (count > 0) {
                if (refreshReg) {
                    refresh();
                    return;
                }
                count--;
                bt_verification.setText(count + "秒后重发");
                handlerBtn.postDelayed(runnable, 1000);
            } else if (count == 0) {
                refresh();
                return;
            }
        }


    };

    private void refresh() {
        count = 60;
        bt_verification.setText("发送验证码");
        bt_verification.setClickable(true);
        bt_verification.setTextColor(Color.WHITE);
        bt_verification.setBackgroundResource(R.drawable.corner_bg2);
        handlerBtn.removeCallbacks(runnable);
        return;
    }
}
